/**
 * 20220304:[require] to [import]...jordia
 */
import {Texture,LinearFilter} from 'three';
import Text from '../graphic/text';

/**
 * 带数字下标的文字
 */
export default class SubText extends Text{
    constructor(values={}){
        super(values);
    }

    /**
     * 重写绘制文本方法
     * @private
     */
    _drawText(){
        let canvas=document.createElement('canvas');
        let _fontSize = this._props.fontSize.value * this._props.pixelRatio.value  * this._props.vivid.value;
        let ratBasic = 100 * 16 / this._props.fontSize.value * this._props.vivid.value;
        let ctx = canvas.getContext('2d');
        ctx.font = '' + this._props.fontStyle.value + ' ' + _fontSize + 'px ' + this._props.fontFamily.value + '';
        let textT=' '+this._props.text.value+' ';
        let ta=textT.split('');
        canvas.width = ctx.measureText(textT).width;
        canvas.height = _fontSize * 1.5;
        let ratX = canvas.width / ratBasic;
        let ratY = canvas.height / canvas.width * ratX;
        if(this._props.background.value){
            ctx.rect(0,0,canvas.width,canvas.height);
            ctx.fillStyle='#'+this._props.background.value.getHexString();
            ctx.fill();
        }
        if(this._props.border.value){
            let w=canvas.height*0.04;
            ctx.lineWidth=w;
            ctx.moveTo(w*0.5,w*0.5);
            ctx.lineTo(w*0.5,canvas.height-w*0.5);
            ctx.lineTo(canvas.width-w*0.5,canvas.height-w*0.5);
            ctx.lineTo(canvas.width-w*0.5,w*0.5);
            ctx.lineTo(w*0.5,w*0.5);
            ctx.lineTo(w*0.5,canvas.height*0.5);
            ctx.strokeStyle='#'+this._props.border.value.getHexString();
            ctx.stroke();
        }
        let cw=0;
        for(let i=0;i<ta.length;i++) {
            let str=ta[i];
            let reg=/[0-9]/g;
            if (!reg.test(str)) {
                ctx.font = 'italic ' + _fontSize + 'px Times New Roman';
                ctx.fillStyle = '#'+this._props.color.value.getHexString();
                ctx.fillText(str, cw, _fontSize * 1.1);
                cw+=ctx.measureText(str).width;
            } else {
                ctx.font = 'normal ' + _fontSize*0.7 + 'px Times New Roman';
                ctx.fillStyle = '#'+this._props.color.value.getHexString();
                ctx.fillText(str, cw, _fontSize * 1.3);
                cw+=ctx.measureText(str).width;
            }
        }
        this.material.map = new Texture(canvas);
        this.material.map.minFilter = LinearFilter;
        this.material.map.magFilter = LinearFilter;
        this.material.map.needsUpdate = true;
        this.scale.set(ratX, ratY, 1);
    }
}